<?php
/**
 * @CreateTime:   2020/8/19 12:30 上午
 * @Author:       huizhang  <2788828128@qq.com>
 * @Copyright:    copyright(2020) Easyswoole all rights reserved
 * @Description:  用户控制器
 */
namespace App\HttpController;

use EasySwoole\EasySwoole\Config;
use EasySwoole\Http\AbstractInterface\Controller;
use EasySwoole\HttpClient\HttpClient;
class Index extends Controller
{

    /**
     * 用户信息
     *
     * @return string
     * CreateTime: 2020/8/19 12:37 上午
     */
    public function index()
    {
        
	$post = $this->request()->getRequestParam();
        $post = json_decode($post,true);
        #$post = json_decode('{"total":2,"data":[{"t":"长沙机场建行龙腾接送机查证件吗","u":"https://www.flyertea.com/thread-3952648-1-1.html"},{"t":"下卡了，开心","u":"https://www.flyertea.com/thread-3952646-1-1.html"},{"t":"卡卡监控提醒","u":"http://www.kakawz.com"}]}',true);
        if($post['total'] < 1){
            return ;
        }
        $content = "";
        foreach ($post['data'] as $item){
            if($item['t'] == "卡卡监控提醒" || $item['u'] == "http://www.kakawz.com" ){
                continue ;
            }
            $content .= "\n".$item['t'] ."\n" .$item['u'].";\n";
        }
        if (mb_strlen($content) >= 2048 ){

            $contents = explode(";",$content);
            foreach ($contents as $content){

                $this->push($content);
            }

            return ;

        }

        $this->push($content);
    }


    public function push($content){

        $msg = [
            'msgtype' => 'text',
            "text" => [
                'content' => $content,
                "mentioned_list"=> ["@all"],
            ]

        ];
        $key = Config::getInstance()->getConf("qy_wechat_key");
        var_dump($msg);
        $http = new HttpClient("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={$key}");
        $res = $http->postJson(json_encode($msg));
        var_dump($res->getBody());
    }

    /**
     * 接口请求量
     *
     * CreateTime: 2020/8/19 12:37 上午
     */
    public function requestTotal()
    {
        $this->response()->write('请求数+1' . PHP_EOL);

        // 还可以 return，但不要两个方法互相调用，会导致死循环
    }

    /**
     * 此控制器抛异常时会执行此方法
     *
     * @param \Throwable $throwable
     * @throws \Throwable
     * CreateTime: 2020/8/19 12:48 上午
     */
    public function onException(\Throwable $throwable): void
    {
        parent::onException($throwable); // TODO: Change the autogenerated stub
    }

    /**
     * gc 方法将在执行完 afterAction 方法之后自动调用，可自行覆盖实现其他的 gc 回收逻辑
     *
     * CreateTime: 2020/8/19 12:52 上午
     */
    public function gc()
    {
        parent::gc(); // TODO: Change the autogenerated stub
    }

    /**
     * 当控制器方法执行结束之后将调用该方法，可自行覆盖该方法实现数据回收等逻辑
     *
     * @param string|null $actionName
     * CreateTime: 2020/8/19 12:51 上午
     */
    public function afterAction(?string $actionName): void
    {
        parent::afterAction($actionName); // TODO: Change the autogenerated stub
    }

    /**
     * 当请求方法未找到时，自动调用该方法，可自行覆盖该方法实现自己的逻辑
     *
     * @param string|null $action
     * CreateTime: 2020/8/19 12:51 上午
     */
    public function actionNotFound(?string $action)
    {
        parent::actionNotFound($action); // TODO: Change the autogenerated stub
    }

    /**
     * 所有控制器请求都会先经过该方法，如果此方法返回 false 则请求不继续往下执行，可用于权限验证
     *
     * @param string|null $action
     * @return bool|null
     * CreateTime: 2020/8/19 12:52 上午
     */
    public function onRequest(?string $action): ?bool
    {
        return parent::onRequest($action); // TODO: Change the autogenerated stub
    }

}
